package leetcode;

/**
 * https://leetcode-cn.com/problems/number-complement/description/
 * https://leetcode.com/problems/number-complement/?tab=Description
 * @program: brushing
 * @description:
 * @author: LiuQiang
 * @create: 2018-07-25 10:35
 **/
public class NumberComplement {
    public int findComplement(int num) {
        String numBinary = Integer.toBinaryString(num);
        String nbs[] = numBinary.split("");
        for (int i = 0; i < nbs.length; i++) {
            if ("0".equals(nbs[i])) {
                nbs[i] = "1";
            } else {
                nbs[i] = "0";
            }
        }
        StringBuffer sb = new StringBuffer();
        for (int i = 0; i < nbs.length; i++) {
            sb.append(nbs[i]);
        }
        return Integer.valueOf(Integer.valueOf(new String(sb), 2).toString());
    }

    public static void main(String[] args) {
        System.out.println(new NumberComplement().findComplement(5));
    }
}
